#!/bin/bash
# get global netblocks
#
# This file is part of MirrorManager2.
#
# The purpose of this script is to download a BGP table of netblocks
# and then write out a file matching those netblocks to ASNs (autonomous system
# numbers).  ASNs are administrative numbers designated by IAANA which define a
# kind of boundary for a set of routers.  For instance, MIT's ASN is 3; every
# router within MIT's sphere of influence bears that ASN.  Every ISP has their
# own, etc.
#
# This script produces one of two netblocks files (along with its sister,
# get_internet2_netblocks) which gets read in by the mirrorlist-server daemon
# and used as part of the larger mirror-matching logic there.  When people
# query the mirrorlist, we can match their IP (potentially) to an ASN to get
# them to a mirror that's closer to them than not.
#
# Author: Matt Domsch <mdomsch@fedoraproject.org>

if [ $# -eq 0 ]; then
    echo "usage: get_global_netblocks <output_filename>" >&2
    exit 1
fi

outfile="$1"
tmpdir=$(mktemp -d /tmp/get_global_netblocks.XXXXXXXX) || exit 1
trap "rm -rf ${tmpdir}" EXIT QUIT HUP KILL TERM
listfile=$tmpdir/list

function last_rib()
{
    head -n 20 ${tmpdir}/index.html | grep rib\. | head -n 1 | \
    awk -F 'href="' '{print $2}' | \
    sed -e 's/".*//'
}

function get_ipv6_netblocks()
{
    local curdate=$(date --date='yesterday' +"%Y.%m")
    URL="http://archive.routeviews.org/route-views6/bgpdata/$curdate/RIBS/"
    wget -q -O ${tmpdir}/index.html "${URL}?C=M;O=D"
    last=$(last_rib)
    wget -O ${tmpdir}/$(basename $last) -q "${URL}/$last"
    bzcat ${tmpdir}/$(basename $last) | perl zebra-dump-parser/zebra-dump-parser.pl | uniq > $tmpdir/ipv6.txt
    # this prefix appears repeatedly for multiple ASs, which is nuts.
    sed -e '/2001::\/32/d' $tmpdir/ipv6.txt >> ${listfile}
}

function get_global_netblocks()
{
    URL='http://ftp.routeviews.org/dnszones/rib.bz2'
    wget -O ${tmpdir}/rib.bz2 -q "${URL}"
    bzcat ${tmpdir}/rib.bz2 | perl zebra-dump-parser/zebra-dump-parser.pl | uniq >> ${listfile}
}

get_global_netblocks
get_ipv6_netblocks
if [ -s ${listfile} ]; then
    cp -f ${listfile} "${outfile}"
    echo "{\"type\": \"global\", \"success\": true}" | fedmsg-logger \
        --cert-prefix mirrormanager \
        --modname mirrormanager \
        --topic "netblocks.get" \
        --json-input
else
    echo "unable to retrieve netblock list." >&2
    echo "{\"type\": \"global\", \"success\": false}" | fedmsg-logger \
        --cert-prefix mirrormanager \
        --modname mirrormanager \
        --topic "netblocks.get" \
        --json-input
    exit 1
fi
exit 0
